package io.gitee.chearnee.fw.common.utils;

import lombok.experimental.UtilityClass;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/11/2022 5:25 PM
 */
@UtilityClass
public class FieldUtil {
    public static final Field[] EMPTY_FIELD_ARRAY = new Field[0];

    public static Field[] getAllFields(final Class<?> cls) {
        final List<Field> allFieldsList = getAllFieldsList(cls);
        return allFieldsList.toArray(EMPTY_FIELD_ARRAY);
    }

    public static List<Field> getAllFieldsList(final Class<?> cls) {
        final List<Field> allFields = new ArrayList<>();
        if (cls == null) {
            return allFields;
        }
        Class<?> currentClass = cls;
        while (currentClass != null) {
            final Field[] declaredFields = currentClass.getDeclaredFields();
            Collections.addAll(allFields, declaredFields);
            currentClass = currentClass.getSuperclass();
        }
        return allFields;
    }

    /**
     * 判断Field是否是一个entity
     *
     * @param field
     * @return
     */
    public static boolean fieldIsEntity(Field field) {
        return ClassUtil.isEntity(field.getType());
    }

    public static boolean isSuperInterface(Class<?> var0, Class<?> var1) {
        Class[] var2 = var0.getInterfaces();
        for (Class var5 : var2) {
            if (var5 == var1) {
                return true;
            }

            if (isSuperInterface(var5, var1)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isSubclassOf(Class<?> var0, Class<?> var1) {
        while (var0 != null) {
            if (var0 == var1) {
                return true;
            }
            var0 = var0.getSuperclass();
        }

        return false;
    }
}
